package br.com.puertorico.mecanismos.ui.utils;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.transaction.TransactionSystemException;

import br.com.puertorico.common.exceptions.ApplicationException;
import br.com.puertorico.common.loggin.Logger;
import br.com.puertorico.common.utils.Formatador;
import br.com.puertorico.common.utils.ResourceBundleUtils;
import br.com.puertorico.model.service.exceptions.DatabaseException;

public class MessagesUtils {

	private static final Logger log = Logger.getLogger(MessagesUtils.class);

	public static String getMessageInApplicationBundle(String key,
			Object... params) {
		try {
			String bundleName = FacesUtils.getServletContext()
					.getInitParameter("br.com.puertorico.APPLICATION_BUNDLE");
			return ResourceBundleUtils.getKey(Formatador.getLocaleBrasil(),
					bundleName, key, params);
		} catch (Exception e) {
			log.warn(
					"Chave '#0' nao encontrada no br.com.puertorico.APPLICATION_BUNDLE",
					key);
			return key;
		}
	}

	public static void addInfoMessage(String summary, String detail,
			Object... params) {
		summary = getMessageInApplicationBundle(summary, params);
		detail = getMessageInApplicationBundle(detail, params);
		FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO,
				summary, detail);
		FacesContext.getCurrentInstance().addMessage(null, facesMsg);
	}

	public static void addErrorMessage(String summary, String detail,
			Object... params) {
		summary = getMessageInApplicationBundle(summary, params);
		detail = getMessageInApplicationBundle(detail, params);
		FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR,
				summary, detail);
		FacesContext.getCurrentInstance().addMessage(null, facesMsg);
	}

	public static void addFatalMessage(String summary, String detail,
			Object... params) {
		summary = getMessageInApplicationBundle(summary, params);
		detail = getMessageInApplicationBundle(detail, params);
		FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL,
				summary, detail);
		FacesContext.getCurrentInstance().addMessage(null, facesMsg);
	}

	public static void addErrorMessage(Exception e) {
		String summary = "";
		String detail = "";

		Throwable root = ExceptionUtils.getRootCause(e);

		if (e instanceof TransactionSystemException) {
			TransactionSystemException tse = (TransactionSystemException) e;
			if (tse.getApplicationException() instanceof DatabaseException) {
				DatabaseException dbex = (DatabaseException) tse
						.getApplicationException();
				summary = dbex.getSummary();
				detail = dbex.getDetail();
				addErrorMessage(summary, detail, (Object[]) dbex.getParams());
				return;
			}
		}
		if (root == null) {
			root = e;
		}
		summary = new StringBuilder(root.getClass().getSimpleName()).append(
				".summary").toString();
		detail = new StringBuilder(root.getClass().getSimpleName()).append(
				".detail").toString();
		Object[] params = new Object[] { "" };
		if (e instanceof ApplicationException) {
			ApplicationException a = (ApplicationException) e;
			params = (Object[]) a.getParams();
		}
		addErrorMessage(summary, detail, params);
	}

	public static void addWarningMessage(String summary, String detail,
			Object... params) {
		summary = getMessageInApplicationBundle(summary, params);
		detail = getMessageInApplicationBundle(detail, params);
		FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_WARN,
				summary, detail);
		FacesContext.getCurrentInstance().addMessage(null, facesMsg);
	}

	public static <T extends Enum<?>> String resolveEnum(final T enumValue) {
		if (enumValue == null) {
			return "";
		}
		StringBuilder str = new StringBuilder(enumValue.getClass()
				.getSimpleName());
		str.append(".").append(enumValue.toString());

		try {
			String bundleName = FacesUtils.getServletContext()
					.getInitParameter("br.com.puertorico.ENUM_BUNDLE");
			return ResourceBundleUtils.getKey(Formatador.getLocaleBrasil(),
					bundleName, str.toString());
		} catch (Exception e) {
			log.warn(
					"Chave '#0' nao encontrada no br.com.puertorico.ENUM_BUNDLE",
					str.toString());
			return str.toString();
		}
	}
}